package com.gewu.bookeeping.View.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gewu.bookeeping.MyDb.bean.MesBeanOut;
import com.gewu.bookeeping.MyDb.dao.MesOutPerson;
import com.gewu.bookeeping.R;
import com.gewu.bookeeping.View.activity.ItemActivity;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class thrFragemnt extends Fragment {

    private Toolbar toolbar;
    private TextView title;
    private RecyclerView recyclerView;
    private List<MesBeanOut> quer;
    private MyAdapter myAdapter;
    private Banner banner;
    private List<Integer> imageViewList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(container.getContext()).inflate(R.layout.activity_thr_fragemnt, container, false);
        initView(inflate);
        setBanner();
        return inflate;
    }

    private void setBanner() {
        imageViewList.add(R.drawable.ic_ali);
        imageViewList.add(R.drawable.ic_mes);
        imageViewList.add(R.drawable.ic_wecahrt);

        banner.setImageLoader(new MyImageLoader());
        //设置轮播的动画效果,里面有很多种特效,可以都看看效果。
        banner.setBannerAnimation(Transformer.ZoomOutSlide);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是true
        banner.isAutoPlay(true);
        //设置图片加载地址
        banner.setImages(imageViewList).start();
    }

    private class MyImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView);
        }
    }

    private void initView(View inflater) {
        toolbar = (Toolbar) inflater.findViewById(R.id.toolbar);
        title = (TextView) inflater.findViewById(R.id.title);
        recyclerView = inflater.findViewById(R.id.recliview);
        banner = inflater.findViewById(R.id.banner);
        toolbar.setTitle("收藏列表");
        toolbar.setLogo(R.mipmap.thr_ic);

        MesOutPerson mesOutPerson = new MesOutPerson(getContext());
        quer = mesOutPerson.quer();

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);
    }

    public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_item_content, parent, false);
            return new ItemContent(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ((ItemContent) holder).name.setText(quer.get(position).getText());
            ((ItemContent) holder).time.setText(quer.get(position).getTime());
            ((ItemContent) holder).type.setText(quer.get(position).getType());
            ((ItemContent) holder).tab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ItemActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("values", 0);
                    bundle.putInt("type", 1);
                    intent.putExtra("date", bundle);
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return quer == null ? 0 : quer.size();
        }
    }

    private class ItemContent extends RecyclerView.ViewHolder {

        TextView name;
        TextView time;
        TextView type;
        LinearLayout tab;

        public ItemContent(View inflate) {
            super(inflate);
            name = inflate.findViewById(R.id.name);
            time = inflate.findViewById(R.id.time);
            type = inflate.findViewById(R.id.type);
            tab = inflate.findViewById(R.id.tab);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            quer.clear();
            MesOutPerson mesOutPerson = new MesOutPerson(getContext());
            quer = mesOutPerson.quer();
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
            myAdapter = new MyAdapter();
            recyclerView.setAdapter(myAdapter);
            myAdapter.notifyDataSetChanged();
        }
    }
}